package com.yueka.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yueka.mapper.SystemUserMapper;
import com.yueka.pojo.PageResult;
import com.yueka.pojo.system_user.LoginInfo;
import com.yueka.pojo.system_user.SystemUser;
import com.yueka.service.SystemUserService;
import com.yueka.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemUserServiceImpl implements SystemUserService {
    @Value("${musicSystem.defaultPassword}")
    private String DEFAULT_PASSWORD;

    @Autowired
    private SystemUserMapper systemUserMapper;

    public LoginInfo login(SystemUser systemUser) {
        LoginInfo loginInfo = systemUserMapper.getSystemUserByUsernameAndPassword(systemUser);
        if (loginInfo == null) {
            return null;
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", loginInfo.getId());
        claims.put("username", loginInfo.getUsername());
        String token = JwtUtils.generateToken(loginInfo.getId().toString(), claims);
        loginInfo.setToken(token);
        return loginInfo;
    }

    @Override
    public Integer register(SystemUser systemUser) {
        systemUser.setCreateTime(LocalDateTime.now());
        systemUser.setUpdateTime(LocalDateTime.now());
        return systemUserMapper.register(systemUser);
    }

    @Override
    public PageResult<SystemUser> getList(String keyword, String status, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<SystemUser> list = systemUserMapper.getList(keyword, status);
        Page<SystemUser> p = (Page<SystemUser>) list;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public Integer update(SystemUser systemUser) {
        systemUser.setUpdateTime(LocalDateTime.now());
        if (systemUser.getId() == null) {
            systemUser.setCreateTime(LocalDateTime.now());
            return systemUserMapper.insert(systemUser);
        }
        return systemUserMapper.update(systemUser);
    }

    @Override
    public Integer delete(Integer[] ids) {
        return systemUserMapper.delete(ids);
    }

    @Override
    @Transactional
    public void updatePassword(String oldPassword, String newPassword, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.replace("Bearer ", "");
        Claims claims = JwtUtils.parseToken(token);
        Integer id = (Integer) claims.get("id");
        SystemUser systemUser = systemUserMapper.verificationPassword(id, oldPassword);
        if (systemUser == null) {
            throw new RuntimeException("原密码错误");
        }
        systemUserMapper.updatePassword(id, newPassword);
    }

    @Override
    public void updatePersonal(SystemUser systemUser , HttpServletRequest request) {
        systemUser.setUpdateTime(LocalDateTime.now());
        String token = request.getHeader("Authorization");
        token = token.replace("Bearer ", "");
        Claims claims = JwtUtils.parseToken(token);
        Integer id = (Integer) claims.get("id");
        systemUser.setId(id);
        systemUserMapper.updatePersonal(systemUser);
    }

    @Override
    public void resetPassword(Integer id) {
        System.out.println(DEFAULT_PASSWORD);
        systemUserMapper.resetPassword(id, DEFAULT_PASSWORD);
    }
}
